<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反馈类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="反馈类型" clearable style="width: 200px">
          <el-option label="功能建议" value="功能建议" />
          <el-option label="问题反馈" value="问题反馈" />
          <el-option label="其他" value="其他" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 200px">
          <el-option label="待处理" value="0" />
          <el-option label="已处理" value="1" />
          <el-option label="已关闭" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="feedbackId" width="80" />
      <el-table-column label="用户名称" align="center" prop="userName" width="120" />
      <el-table-column label="用户昵称" align="center" prop="nickName" width="120" />
      <el-table-column label="反馈类型" align="center" prop="type" width="100" />
      <el-table-column label="反馈内容" align="center" prop="content" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <el-tag v-if="scope.row.status === '0'" type="warning">待处理</el-tag>
          <el-tag v-else-if="scope.row.status === '1'" type="success">已处理</el-tag>
          <el-tag v-else-if="scope.row.status === '2'" type="info">已关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="回复内容" align="center" prop="replyContent" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="回复时间" align="center" prop="replyTime" width="180" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleReply(scope.row)"
            v-hasPermi="['system:feedback:reply']"
            v-if="scope.row.status === '0'"
          >回复</el-button>
          <el-button
            link
            type="info"
            icon="View"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            link
            type="danger"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 回复对话框 -->
    <el-dialog title="回复意见" v-model="replyOpen" width="600px" append-to-body>
      <el-form ref="replyRef" :model="replyForm" :rules="replyRules" label-width="100px">
        <el-form-item label="用户名称">
          <el-input v-model="replyForm.userName" disabled />
        </el-form-item>
        <el-form-item label="反馈类型">
          <el-input v-model="replyForm.type" disabled />
        </el-form-item>
        <el-form-item label="反馈内容">
          <el-input v-model="replyForm.content" type="textarea" :rows="4" disabled />
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <el-input
            v-model="replyForm.replyContent"
            type="textarea"
            :rows="6"
            placeholder="请输入回复内容"
            maxlength="500"
            show-word-limit
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitReply">确 定</el-button>
          <el-button @click="cancelReply">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看对话框 -->
    <el-dialog title="查看意见" v-model="viewOpen" width="600px" append-to-body>
      <el-form ref="viewRef" :model="viewForm" label-width="100px">
        <el-form-item label="用户名称">
          <el-input v-model="viewForm.userName" disabled />
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="viewForm.nickName" disabled />
        </el-form-item>
        <el-form-item label="反馈类型">
          <el-input v-model="viewForm.type" disabled />
        </el-form-item>
        <el-form-item label="反馈内容">
          <el-input v-model="viewForm.content" type="textarea" :rows="4" disabled />
        </el-form-item>
        <el-form-item label="状态">
          <el-tag v-if="viewForm.status === '0'" type="warning">待处理</el-tag>
          <el-tag v-else-if="viewForm.status === '1'" type="success">已处理</el-tag>
          <el-tag v-else-if="viewForm.status === '2'" type="info">已关闭</el-tag>
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input v-model="viewForm.createTime" disabled />
        </el-form-item>
        <el-form-item label="回复内容" v-if="viewForm.replyContent">
          <el-input v-model="viewForm.replyContent" type="textarea" :rows="4" disabled />
        </el-form-item>
        <el-form-item label="回复时间" v-if="viewForm.replyTime">
          <el-input v-model="viewForm.replyTime" disabled />
        </el-form-item>
        <el-form-item label="回复人" v-if="viewForm.replyByName">
          <el-input v-model="viewForm.replyByName" disabled />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="viewOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Feedback">
import { ref, onMounted, getCurrentInstance } from "vue";
import { listFeedback, getFeedback, delFeedback, replyFeedback } from "@/api/system/feedback";

const { proxy } = getCurrentInstance();

const feedbackList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const replyOpen = ref(false);
const viewOpen = ref(false);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  userName: null,
  type: null,
  status: null
});

const replyForm = ref({});
const viewForm = ref({});

const replyRules = {
  replyContent: [
    { required: true, message: "回复内容不能为空", trigger: "blur" },
    { min: 5, max: 500, message: "回复内容长度在 5 到 500 个字符", trigger: "blur" }
  ]
};

/** 查询反馈列表 */
function getList() {
  loading.value = true;
  listFeedback(queryParams.value).then(response => {
    feedbackList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancelReply() {
  replyOpen.value = false;
  resetReply();
}

// 表单重置
function resetReply() {
  replyForm.value = {
    feedbackId: null,
    userName: null,
    type: null,
    content: null,
    replyContent: null
  };
  proxy.resetForm("replyRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.feedbackId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 回复按钮操作 */
function handleReply(row) {
  resetReply();
  const feedbackId = row.feedbackId || ids.value[0];
  getFeedback(feedbackId).then(response => {
    replyForm.value = response.data;
    replyOpen.value = true;
  });
}

/** 查看按钮操作 */
function handleView(row) {
  const feedbackId = row.feedbackId;
  getFeedback(feedbackId).then(response => {
    viewForm.value = response.data;
    viewOpen.value = true;
  });
}

/** 提交回复 */
function submitReply() {
  proxy.$refs["replyRef"].validate(valid => {
    if (valid) {
      replyFeedback(replyForm.value).then(response => {
        proxy.$modal.msgSuccess("回复成功");
        replyOpen.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const feedbackIds = row.feedbackId || ids.value;
  proxy.$modal.confirm('是否确认删除反馈编号为"' + feedbackIds + '"的数据项？').then(function() {
    return delFeedback(feedbackIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

onMounted(() => {
  getList();
});
</script>

